package cn.wolfcode.controller;


import cn.wolfcode.wolf2w.JsonResult.JsonResult;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")RegionQuery qo){
        Page<Region> page=regionService.query(qo);
        model.addAttribute("page",page);
        //pageinfo:MyBatis插件，操作的是Mysql数据库
        //page：spring-data 的组件，操作的是mongodb数据库
        return "region/list";
    }


    //目的地下拉框数据
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }


    //添加功能
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    //关联地区
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String  id){
        List<Destination> list=destinationService.getDestByRegionId(id);
        return list;
    }

    //热门操作
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String  id,int hot){
        regionService.changeHotValue(id,hot);
        return JsonResult.success();
    }
}
